<!DOCTYPE html>
<html lang="zh-cn">
<head>
  <title>View 模板渲染 - 为企业级框架和应用而生</title>
  <meta charset="utf-8">
  <meta name="description" content="index.description">
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
  <link rel="icon" href="/images/favicon.png" type="image/x-icon">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.css" />
<link rel="stylesheet" href="/css/index.css">

    <script>
    !function(t,e,a,r,c){t.TracertCmdCache=t.TracertCmdCache||[],t[c]=window[c]||
      {_isInit:!0,call:function(){t.TracertCmdCache.push(arguments)},
      start:function(t){this.call('start',t)}},t[c].l=new Date;
      var n=e.createElement(a),s=e.getElementsByTagName(a)[0];
      n.async=!0,n.src=r,s.parentNode.insertBefore(n,s)}
    (window,document,'script','https://tracert.alipay.com/tracert.js','Tracert');
      Tracert.start({
        plugins: [ 'BucName' ],
        spmAPos: 'a454',
        spmBPos: 'b4893',
      });
    </script>
  
</head>
<body>
  <div class="nav" >
  <header>
    <a href="/zh-cn/" class="nav-logo leftpadding" alt="egg"><img src="https://zos.alipayobjects.com/rmsportal/VTcUYAaoKqXyHJbLAPyF.svg"></a>
    <ul class="nav-item">
      <li>
        <form id="search-form">
          <input type="text" id="search-query" class="search-query st-default-search-input">
        </form>
      </li>
      <li><a href="/zh-cn/intro/" alt="指南">指南</a></li><li><a href="/api/" alt="API">API</a></li><li><a href="/zh-cn/tutorials/index.html" alt="教程">教程</a></li><li><a href="https://github.com/search?q=topic%3Aegg-plugin&type=Repositories" alt="插件">插件</a></li><li><a href="https://github.com/eggjs/egg/releases" alt="发布日志">发布日志</a></li>
      
      
        <li class="translations">
          <a class="nav-link">Translations</a>
          <span class="arrow"></span><ul id="dropdownContent" class="dropdown-content"><li><a id="en" href="/en/core/view.html" >English</a></li><li><a id="zh-cn" href="/zh-cn/core/view.html" style="color: #22ab28">中文</a></li></ul>
        </li>
      
      <li><iframe src="https://ghbtns.com/github-btn.html?user=eggjs&repo=egg&type=star&count=true" frameborder="0" scrolling="0" width="150px" height="20px"></iframe></li>
    </ul>
    <a id="mobileTrigger" href="#" class="mobile-trigger">
      <ul>
        <li></li>
        <li></li>
        <li></li>
      </ul>
    </a>
  </header>
</div>
  <div id="container" class="container">
    <div class="page-main">
  <article class="markdown-body">
    <h1>View 模板渲染</h1>
    <p>绝大多数情况，我们都需要读取数据后渲染模板，然后呈现给用户。故我们需要引入对应的模板引擎。</p>
<p>框架内置 <a href="https://github.com/eggjs/egg-view" target="_blank" rel="noopener">egg-view</a> 作为模板解决方案，并支持多模板渲染，每个模板引擎都以插件的方式引入，但保持渲染的 API 一致。如果想更深入的了解，可以查看<a href="../advanced/view-plugin.html">模板插件开发</a>。</p>
<p>以下以官方支持的 View 插件 <a href="https://github.com/eggjs/egg-view-nunjucks" target="_blank" rel="noopener">egg-view-nunjucks</a> 为例</p>
<h2 id="引入-view-插件"><a class="markdown-anchor" href="#引入-view-插件">#</a> 引入 view 插件</h2>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ npm i egg-view-nunjucks --save</span><br></pre></td></tr></table></figure>
<h3 id="启用插件"><a class="markdown-anchor" href="#启用插件">#</a> 启用插件</h3>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// config/plugin.js</span></span><br><span class="line">exports.nunjucks = &#123;</span><br><span class="line">  enable: <span class="literal">true</span>,</span><br><span class="line">  package: <span class="string">'egg-view-nunjucks'</span>,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h2 id="配置插件"><a class="markdown-anchor" href="#配置插件">#</a> 配置插件</h2>
<p><a href="https://github.com/eggjs/egg-view" target="_blank" rel="noopener">egg-view</a> 提供了 <code>config.view</code> 通用配置</p>
<h3 id="root-string"><a class="markdown-anchor" href="#root-string">#</a> root {String}</h3>
<p>模板文件的根目录，为绝对路径，默认为 <code>${baseDir}/app/view</code>。支持配置多个目录，以 <code>,</code> 分割，会从多个目录查找文件。</p>
<p>如下示例演示了如何配置多个 <code>view</code> 目录：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// config/config.default.js</span></span><br><span class="line"><span class="keyword">const</span> path = <span class="built_in">require</span>(<span class="string">'path'</span>);</span><br><span class="line"><span class="built_in">module</span>.exports = <span class="function"><span class="params">appInfo</span> =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> config = &#123;&#125;;</span><br><span class="line">  config.view = &#123;</span><br><span class="line">    root: [</span><br><span class="line">      path.join(appInfo.baseDir, <span class="string">'app/view'</span>),</span><br><span class="line">      path.join(appInfo.baseDir, <span class="string">'path/to/another'</span>),</span><br><span class="line">    ].join(<span class="string">','</span>)</span><br><span class="line">  &#125;;</span><br><span class="line">  <span class="keyword">return</span> config;</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<h3 id="cache-boolean"><a class="markdown-anchor" href="#cache-boolean">#</a> cache {Boolean}</h3>
<p>模板路径缓存，默认开启。框架会根据 root 配置的目录依次查找，如果匹配则会缓存文件路径，下次渲染相同路径时不会重新查找。</p>
<h3 id="mapping-和-defaultviewengine"><a class="markdown-anchor" href="#mapping-和-defaultviewengine">#</a> mapping 和 defaultViewEngine</h3>
<p>每个模板在注册时都会指定一个模板名（viewEngineName），在使用时需要根据后缀来匹配模板名，比如指定 <code>.nj</code> 后缀的文件使用 Nunjucks 进行渲染。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  view: &#123;</span><br><span class="line">    mapping: &#123;</span><br><span class="line">      <span class="string">'.nj'</span>: <span class="string">'nunjucks'</span>,</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>调用 render 渲染文件时，会根据上述配置的后缀名去寻找对应的模板引擎。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">await</span> ctx.render(<span class="string">'home.nj'</span>);</span><br></pre></td></tr></table></figure>
<p>必须配置文件后缀和模板引擎的映射，否则无法找到对应的模板引擎，但是可以使用 <code>defaultViewEngine</code> 做全局配置。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// config/config.default.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  view: &#123;</span><br><span class="line">    defaultViewEngine: <span class="string">'nunjucks'</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br></pre></td></tr></table></figure>
<p>如果根据文件后缀没有找到对应的模板引擎，会使用默认的模板引擎进行渲染。对于只使用一种模板引擎的应用，建议配置此选项。</p>
<h3 id="defaultextension"><a class="markdown-anchor" href="#defaultextension">#</a> defaultExtension</h3>
<p>一般在调用 render 时的第一个参数需要包含文件后缀，如果配置了 defaultExtension 可以省略后缀。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// config/config.default.js</span></span><br><span class="line"><span class="built_in">module</span>.exports = &#123;</span><br><span class="line">  view: &#123;</span><br><span class="line">    defaultExtension: <span class="string">'.nj'</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// render app/view/home.nj</span></span><br><span class="line"><span class="keyword">await</span> ctx.render(<span class="string">'home'</span>);</span><br></pre></td></tr></table></figure>
<h2 id="渲染页面"><a class="markdown-anchor" href="#渲染页面">#</a> 渲染页面</h2>
<p>框架在 Context 上提供了 3 个接口，返回值均为 Promise:</p>
<ul>
<li><code>render(name, locals)</code> 渲染模板文件, 并赋值给 ctx.body</li>
<li><code>renderView(name, locals)</code> 渲染模板文件, 仅返回不赋值</li>
<li><code>renderString(tpl, locals)</code> 渲染模板字符串, 仅返回不赋值</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// &#123;app_root&#125;/app/controller/home.js</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HomeController</span> <span class="keyword">extends</span> <span class="title">Controller</span> </span>&#123;</span><br><span class="line">  <span class="keyword">async</span> index() &#123;</span><br><span class="line">    <span class="keyword">const</span> data = &#123; <span class="attr">name</span>: <span class="string">'egg'</span> &#125;;</span><br><span class="line"></span><br><span class="line">    <span class="comment">// render a template, path relate to `app/view`</span></span><br><span class="line">    <span class="keyword">await</span> ctx.render(<span class="string">'home/index.tpl'</span>, data);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// or manually set render result to ctx.body</span></span><br><span class="line">    ctx.body = <span class="keyword">await</span> ctx.renderView(<span class="string">'path/to/file.tpl'</span>, data);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// or render string directly</span></span><br><span class="line">    ctx.body = <span class="keyword">await</span> ctx.renderString(<span class="string">'hi, &#123;&#123; name &#125;&#125;'</span>, data, &#123;</span><br><span class="line">      viewEngine: <span class="string">'nunjucks'</span>,</span><br><span class="line">    &#125;);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>当使用 <code>renderString</code> 时需要指定模板引擎，如果已经定义 <code>defaultViewEngine</code> 这里可以省略。</p>
<h2 id="locals"><a class="markdown-anchor" href="#locals">#</a> Locals</h2>
<p>在渲染页面的过程中，我们通常需要一个变量来收集需要传递给模板的变量，在框架里面，我们提供了 <code>app.locals</code> 和 <code>ctx.locals</code>。</p>
<ul>
<li><code>app.locals</code> 为全局的，一般在 <code>app.js</code> 里面配置全局变量。</li>
<li><code>ctx.locals</code> 为单次请求的，会合并 <code>app.locals</code>。</li>
<li>可以直接赋值对象，框架在对应的 setter 里面会自动 merge。</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// `app.locals` 会合并到 `ctx.locals</span></span><br><span class="line">ctx.app.locals = &#123; <span class="attr">a</span>: <span class="number">1</span> &#125;;</span><br><span class="line">ctx.locals.b = <span class="number">2</span>;</span><br><span class="line"><span class="built_in">console</span>.log(ctx.locals); <span class="comment">// &#123; a: 1, b: 2 &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 一次请求过程中，仅会在第一次使用 `ctx.locals` 时把 `app.locals` 合并进去。</span></span><br><span class="line">ctx.app.locals = &#123; <span class="attr">a</span>: <span class="number">2</span> &#125;;</span><br><span class="line"><span class="built_in">console</span>.log(ctx.locals); <span class="comment">// 上面已经合并过一次，故输出还是 &#123; a: 1, b: 2 &#125;</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 也可以直接赋值整个对象，不用担心会覆盖前面的值，我们通过 setter 做了自动合并。</span></span><br><span class="line">ctx.locals.c = <span class="number">3</span>;</span><br><span class="line">ctx.locals = &#123; <span class="attr">d</span>: <span class="number">4</span> &#125;;</span><br><span class="line"><span class="built_in">console</span>.log(ctx.locals); <span class="comment">// &#123; a: 1, b: 2, c: 3, d: 4 &#125;</span></span><br></pre></td></tr></table></figure>
<p>但在实际业务开发中，controller 中一般不会直接使用这 2 个对象，直接使用 <code>ctx.render(name, data)</code> 即可：</p>
<ul>
<li>框架会自动把 <code>data</code> 合并到 <code>ctx.locals</code>。</li>
<li>框架会自动注入 <code>ctx</code>, <code>request</code>, <code>helper</code> 方便使用。</li>
</ul>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line">ctx.app.locals = &#123; <span class="attr">appName</span>: <span class="string">'showcase'</span> &#125;;</span><br><span class="line"><span class="keyword">const</span> data = &#123; <span class="attr">name</span>: <span class="string">'egg'</span> &#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">// will auto merge `data` to `ctx.locals`, output: egg - showcase</span></span><br><span class="line"><span class="keyword">await</span> ctx.renderString(<span class="string">'&#123;&#123; name &#125;&#125; - &#123;&#123; appName &#125;&#125;'</span>, data);</span><br><span class="line"></span><br><span class="line"><span class="comment">// helper, ctx, request will auto inject</span></span><br><span class="line"><span class="keyword">await</span> ctx.renderString(<span class="string">'&#123;&#123; name &#125;&#125; - &#123;&#123; helper.lowercaseFirst(ctx.app.config.baseDir) &#125;&#125;'</span>, data);</span><br></pre></td></tr></table></figure>
<p>注意：</p>
<ul>
<li><strong>ctx.locals 有缓存，只在第一次访问 ctx.locals 时合并 app.locals。</strong></li>
<li>原 Koa 中的 <code>ctx.state</code>，由于容易产生歧义，在框架中被覆盖为 locals，即 <code>ctx.state</code> 和 <code>ctx.locals</code> 等价，我们建议使用后者。</li>
</ul>
<h2 id="helper"><a class="markdown-anchor" href="#helper">#</a> Helper</h2>
<p>在模板中可以直接使用 <code>helper</code> 上注册的方法，具体可以参见<a href="../basics/extend.html">扩展</a>。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// app/extend/helper.js</span></span><br><span class="line">exports.lowercaseFirst = <span class="function"><span class="params">str</span> =&gt;</span> str[<span class="number">0</span>].toLowerCase() + str.substring(<span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// app/controller/home.js</span></span><br><span class="line"><span class="keyword">await</span> ctx.renderString(<span class="string">'&#123;&#123; helper.lowercaseFirst(name) &#125;&#125;'</span>, data);</span><br></pre></td></tr></table></figure>
<h2 id="security"><a class="markdown-anchor" href="#security">#</a> Security</h2>
<p>框架内置的 <a href="https://github.com/eggjs/egg-security" target="_blank" rel="noopener">egg-security</a> 插件，为我们提供了常见的安全辅助函数，包括 <code>helper.shtml / surl / sjs</code> 等等等，强烈建议阅读下<a href="./security.html">安全</a>。</p>

  </article>
  <aside id="mobileAside" class="toc">
  <div class="mobile-menu">
    <ul>
      <li><a href="/zh-cn/intro/" alt="指南">指南</a></li><li><a href="/api/" alt="API">API</a></li><li><a href="/zh-cn/tutorials/index.html" alt="教程">教程</a></li><li><a href="https://github.com/search?q=topic%3Aegg-plugin&type=Repositories" alt="插件">插件</a></li><li><a href="https://github.com/eggjs/egg/releases" alt="发布日志">发布日志</a></li>
      
      
        <li class="translations">
          <a class="nav-link">Translations</a>
          <span class="arrow"></span><ul id="dropdownContent" class="dropdown-content"><li><a id="en" href="/en/core/view.html" >English</a></li><li><a id="zh-cn" href="/zh-cn/core/view.html" style="color: #22ab28">中文</a></li></ul>
        </li>
      
    </ul>
  </div>
  <dl><dt id="title-Intro" style="cursor: pointer;" class="aside-title">新手指南<a id="collapse-icon-Intro" class="icon opend"></a></dt><dd id=panel-Intro><ul><li><a href="/zh-cn/intro/index.html" class="menu-link">Egg.js 是什么?</a></li><li><a href="/zh-cn/intro/egg-and-koa.html" class="menu-link">Egg.js 和 Koa</a></li><li><a href="/zh-cn/intro/quickstart.html" class="menu-link">快速入门</a></li><li><a href="/zh-cn/tutorials/progressive.html" class="menu-link">渐进式开发</a></li><li><a href="/zh-cn/migration.html" class="menu-link">2.x 升级指南</a></li></ul></dd><dt id="title-Basics" style="cursor: pointer;" class="aside-title">基础功能<a id="collapse-icon-Basics" class="icon opend"></a></dt><dd id=panel-Basics><ul><li><a href="/zh-cn/basics/structure.html" class="menu-link">目录结构</a></li><li><a href="/zh-cn/basics/objects.html" class="menu-link">内置对象</a></li><li><a href="/zh-cn/basics/env.html" class="menu-link">运行环境</a></li><li><a href="/zh-cn/basics/config.html" class="menu-link">配置</a></li><li><a href="/zh-cn/basics/middleware.html" class="menu-link">中间件</a></li><li><a href="/zh-cn/basics/router.html" class="menu-link">Router</a></li><li><a href="/zh-cn/basics/controller.html" class="menu-link">Controller</a></li><li><a href="/zh-cn/basics/service.html" class="menu-link">Service</a></li><li><a href="/zh-cn/basics/plugin.html" class="menu-link">插件</a></li><li><a href="/zh-cn/basics/schedule.html" class="menu-link">定时任务</a></li><li><a href="/zh-cn/basics/extend.html" class="menu-link">框架扩展</a></li><li><a href="/zh-cn/basics/app-start.html" class="menu-link">启动自定义</a></li></ul></dd><dt id="title-Core" style="cursor: pointer;" class="aside-title">核心功能<a id="collapse-icon-Core" class="icon opend"></a></dt><dd id=panel-Core><ul><li><a href="/zh-cn/core/development.html" class="menu-link">本地开发</a></li><li><a href="/zh-cn/core/unittest.html" class="menu-link">单元测试</a></li><li><a href="/zh-cn/core/deployment.html" class="menu-link">应用部署</a></li><li><a href="/zh-cn/core/logger.html" class="menu-link">日志</a></li><li><a href="/zh-cn/core/httpclient.html" class="menu-link">HttpClient</a></li><li><a href="/zh-cn/core/cookie-and-session.html" class="menu-link">Cookie and Session</a></li><li><a href="/zh-cn/core/cluster-and-ipc.html" class="menu-link">多进程模型和进程间通讯</a></li><li><a href="/zh-cn/core/view.html" class="menu-link">模板渲染</a></li><li><a href="/zh-cn/core/error-handling.html" class="menu-link">异常处理</a></li><li><a href="/zh-cn/core/security.html" class="menu-link">安全</a></li><li><a href="/zh-cn/core/i18n.html" class="menu-link">国际化</a></li></ul></dd><dt id="title-Tutorials" style="cursor: pointer;" class="aside-title">教程<a id="collapse-icon-Tutorials" class="icon opend"></a></dt><dd id=panel-Tutorials><ul><li><a href="/zh-cn/tutorials/mysql.html" class="menu-link">MySQL</a></li><li><a href="/zh-cn/tutorials/restful.html" class="menu-link">RESTful API</a></li><li><a href="/zh-cn/tutorials/passport.html" class="menu-link">Passport 鉴权</a></li><li><a href="/zh-cn/tutorials/socketio.html" class="menu-link">Socket.IO</a></li><li><a href="/zh-cn/tutorials/assets.html" class="menu-link">静态资源</a></li><li><a href="/zh-cn/tutorials/typescript.html" class="menu-link">TypeScript</a></li></ul></dd><dt id="title-Advanced" style="cursor: pointer;" class="aside-title">进阶<a id="collapse-icon-Advanced" class="icon opend"></a></dt><dd id=panel-Advanced><ul><li><a href="/zh-cn/advanced/loader.html" class="menu-link">Loader</a></li><li><a href="/zh-cn/advanced/plugin.html" class="menu-link">插件开发</a></li><li><a href="/zh-cn/advanced/framework.html" class="menu-link">框架开发</a></li><li><a href="/zh-cn/advanced/cluster-client.html" class="menu-link">多进程研发模式增强</a></li><li><a href="/zh-cn/advanced/view-plugin.html" class="menu-link">模板插件开发规范</a></li><li><a href="/zh-cn/style-guide.html" class="menu-link">代码风格指南</a></li></ul></dd><dt id="title-Community" style="cursor: pointer;" class="aside-title">社区<a id="collapse-icon-Community" class="icon opend"></a></dt><dd id=panel-Community><ul><li><a href="/zh-cn/plugins/" class="menu-link">内置插件列表</a></li><li><a href="/zh-cn/contributing.html" class="menu-link">如何贡献</a></li><li><a href="/zh-cn/resource.html" class="menu-link">资源</a></li><li><a href="/zh-cn/faq.html" class="menu-link">常见问题</a></li></ul></dd></dl>
</aside>
<script>
var mobileTrigger = document.getElementById('mobileTrigger');
var mobileAside = document.getElementById('mobileAside');

var expandMenu = function(title) {
  // handle icon
  const collapseIcon = document.getElementById('collapse-icon-' + title);
  if (collapseIcon) {
    collapseIcon.className = 'icon opend';
  }
  // handle panelEle
  const panelEle = document.getElementById('panel-' + title);
  if (panelEle) {
    panelEle.className = '';
  }
}

var collapseMenu = function(title) {
  // handle icon
  const collapseIcon = document.getElementById('collapse-icon-' + title);
  if (collapseIcon) {
    collapseIcon.className = 'icon closed';
  }
  // handle panelEle
  const panelEle = document.getElementById('panel-' + title);
  if (panelEle) {
    panelEle.className = 'aside-panel-hidden';
  }
}

mobileAside.onclick = function(e) {
  const targetId = e.target.id;
  if (targetId && (targetId.indexOf('title-') > -1 || targetId.indexOf('collapse-icon-') > -1)) {
    const title = targetId.replace('title-', '').replace('collapse-icon-', '');
    try { 
      // the the browser may have no localStroage or JSON.parse may throw exception.
      const menuInfo = JSON.parse(window.localStorage.getItem('menuInfo'));
        
      // current menu status
      const curClosed = menuInfo[title] ? menuInfo[title].closed : false; // default false

      // change UI
      curClosed ? expandMenu(title) : collapseMenu(title);

      // save menuInfo to localStorage
      menuInfo[title] = { closed: !curClosed } // opposite
      window.localStorage.setItem('menuInfo', JSON.stringify(menuInfo));
    } catch (e) {}
  }
};

mobileTrigger.onclick = function(e) {
  e.preventDefault();
  if (mobileAside.className.indexOf('mobile-show') === -1) {
    mobileAside.className += ' mobile-show';
  } else {
    mobileAside.className = 'toc';
  }
};

(function() {
  // save data to localStorage because the page will refresh when user change the url.
  let menuInfo;
  try { 
    // the the browser may have no localStroage or JSON.parse may throw exception.
    menuInfo = JSON.parse(window.localStorage.getItem('menuInfo'));
    if (!menuInfo) {
      menuInfo = {};
      window.localStorage.setItem('menuInfo', JSON.stringify(menuInfo));
    }
  } catch (e) {
    menuInfo = {}; // default {}
  }

  for (const title in menuInfo) {
    if (menuInfo[title] && menuInfo[title].closed) { // menu in closed status.
      collapseMenu(title);
    } else {
      expandMenu(title);
    }
  }

  // highlight menu
  const pathname = window.location.pathname;
  const selector = `a[href="${pathname}"].menu-link,a[href="${pathname}index.html"].menu-link`;
  const menuItem = mobileAside.querySelector(selector);
  if (menuItem) { menuItem.className += ' highlight'; }
})();
</script>

</div>

  </div>
</body>
<script src="https://cdn.jsdelivr.net/docsearch.js/2/docsearch.min.js"></script>
<script>
docsearch({
  apiKey: '1561de31a86f79507ea00cdb54ce647c',
  indexName: 'eggjs',
  inputSelector: '#search-query',
});
</script>
<div class="cnzz">
<script src="https://s11.cnzz.com/z_stat.php?id=1261142226&web_id=1261142226" language="JavaScript"></script>
</div>

</html>
